package com.aduerr.v1shouyin.unioncard;

import android.graphics.Color;
import android.os.Bundle;
import androidx.annotation.Nullable;
import androidx.annotation.StringRes;
import androidx.appcompat.app.AppCompatActivity;
import android.widget.Toast;
import cn.pedant.SweetAlert.SweetAlertDialog;
import com.aduerr.v1shouyin.util.Utils;
import io.reactivex.disposables.CompositeDisposable;
import io.reactivex.disposables.Disposable;


/**
 * @author HI
 * @email dev1024@foxmail.com
 * @time 2019/7/15 9:39
 * @change
 * @chang time
 * @class describe
 */

public abstract class BaseUnionActivity extends AppCompatActivity implements IView {

    protected CompositeDisposable mCompositeDisposable;
    SweetAlertDialog progressDialog;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if(getLayoutId() != 0) {
            setContentView(getLayoutId());
        }
        mCompositeDisposable = new CompositeDisposable();
        initProgressDialog();
        initBase();
        initView();
    }

    protected void initBase() {

    }

    protected void initProgressDialog() {

        progressDialog = new SweetAlertDialog(this, SweetAlertDialog.PROGRESS_TYPE);
        progressDialog.getProgressHelper().setBarColor(Color.parseColor("#005fee"));
        progressDialog.setTitleText("让数据为你所用");
        progressDialog.setCancelable(false);
    }

    protected abstract int getLayoutId();

    protected void initView() {
    }


    private void showLoadingDialogBySafety(String message, boolean cancelable) {
        progressDialog.setTitleText(message);
        progressDialog.setCancelable(cancelable);
        if(!progressDialog.isShowing()) {
            progressDialog.show();
        }
    }

    public void showLoadingDialog(final String message, final boolean cancelable) {
        if(Utils.isMainThread()) {
            showLoadingDialogBySafety(message, cancelable);
        } else {
            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    showLoadingDialogBySafety(message, cancelable);
                }
            });
        }
    }

    public void showLoadingDialog(String message) {
        showLoadingDialog(message, false);
    }


    public void dismissLoadingDialog() {
        if(progressDialog.isShowing()) {
            if(Utils.isMainThread()) {
                progressDialog.dismiss();
            } else {
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        progressDialog.dismiss();
                    }
                });
            }
        }
    }

    public final void showToast(final CharSequence text, final int duration) {
        if(Utils.isMainThread()) {
            Toast.makeText(this, text, duration).show();
        } else {
            runOnUiThread(() -> Toast.makeText(BaseUnionActivity.this, text, duration).show());
        }
    }

    public final void showToast(@StringRes int resID, int duration) {
        showToast(getString(resID), duration);
    }

    public final void showToast(@StringRes int resID) {
        showToast(resID, Toast.LENGTH_SHORT);
    }

    public final void showToast(CharSequence text) {
        showToast(text, Toast.LENGTH_SHORT);
    }

    public void addDisposable(Disposable d) {
        mCompositeDisposable.add(d);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        mCompositeDisposable.clear();
    }


    @Override
    public void removeDisposable(Disposable d) {
        mCompositeDisposable.remove(d);
    }
}
